package com.lw.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lw.yygh.hosp.repository.DepartmentRepository;
import com.lw.yygh.hosp.service.DepartmentService;
import com.lw.yygh.model.hosp.Department;
import com.lw.yygh.result.Result;
import com.lw.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        //将map集合转换成对象
        Department department = JSONObject.parseObject( JSONObject.toJSONString( map ), Department.class );
        //判断数据库中是否存在该部门 存在就更新 ，不存在既保存
        Department byDepcode = departmentRepository.findByDepcode( department.getDepcode() );
        if (byDepcode!=null){
            BeanUtils.copyProperties( byDepcode,department );
            department.setUpdateTime( new Date() );
            departmentRepository.save( department );
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findAll(Map<String, Object> map) {
        String hoscode = (String)map.get("hoscode");
        int page = StringUtils.isEmpty(map.get("page")) ? 1 : Integer.parseInt((String)map.get("page"));
        int limit = StringUtils.isEmpty(map.get("limit")) ? 10 : Integer.parseInt((String)map.get("limit"));
        //0表示第一页
        PageRequest pageRequest = PageRequest.of( page - 1, limit );
        Department department = new Department();
        department.setHoscode( hoscode );
        Example<Department> departmentExample = Example.of( department );

        return departmentRepository.findAll( departmentExample,pageRequest );
    }

    @Override
    public void remove(Map<String, Object> map) {
        String depcode = (String)map.get("depcode");
        departmentRepository.removeByDepcode(depcode);
    }

    //根据医院编号查找科室
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //
        List<DepartmentVo> list=new ArrayList<>();
        Department department = new Department();
        department.setHoscode( hoscode );
        Example<Department> departmentExample = Example.of( department );
        List<Department> all = departmentRepository.findAll( departmentExample );
        //将all数据变成树形结构封装近list中
        Map<String, List<Department>> collect = all.stream()
                .collect( Collectors.groupingBy( Department::getBigcode ) );
        for (Map.Entry<String,List<Department>> entry:collect.entrySet()){
            String bigcode = entry.getKey();
            List<Department> departmentList = entry.getValue();
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode( bigcode );
            departmentVo.setDepname( departmentList.get( 0 ).getBigname() );
            ArrayList<DepartmentVo> child = new ArrayList<>();
            for (Department department1:departmentList){
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepcode( department1.getDepcode() );
                departmentVo1.setDepname( department1.getDepname() );
                child.add( departmentVo1 );
            }
            //把小科室的list集合放入大科室的children属性中
            departmentVo.setChildren( child );
            list.add( departmentVo );
        }
        return list;
    }
    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

}
